//author:kakake
class tsAdminController {
    public data = {
        dbConn: TSLobsterAPI.Program.GetDbConn()
    }

    public DeptModel = {
        model: {
            DeptId: 0,
            Name: "",
            Pym: "",
            Code: "",
            DelFlag: 0,
            SortOrder: 0,

        },
        tablename: "BaseDept",         //表名
        key: "DeptId",                  //主键字段
        val: "0",                   //主键值
        objval: {},                 //实体值
        where: "",                  //查询条件
        pageinfo: {},               //分页对象 
    }

    public PositionModel = {
        model: {
            ID: 0,
            PositionCode: "",
            PositionName: "",
            DelFlag: 0,
            ParentId: 0,
        },                          //实体模型
        tablename: "BasePosition",         //表名
        key: "ID",                  //主键字段
        val: "0",                   //主键值
        objval: {},                 //实体值
        where: "",                  //查询条件
        pageinfo: {},               //分页对象 
    }

    public SymptomModel = {
        model: {
            SymId: 0,
            Symptom: "",
            ClassId: 0,
            ClassType: 0,
            DelFlag: 0,
            SortOrder: 0,
            DeptCode: ""
        },                          //实体模型
        tablename: "BaseSymptom",         //表名
        key: "SymId",                  //主键字段
        val: "0",                   //主键值
        objval: {},                 //实体值
        where: "",                  //查询条件
        pageinfo: {},               //分页对象 
    }

    public SymptomClassModel = {
        model: {
            ClassId: 0,
            ClassCode: "",
            ClassName: "",
            ParentId: 0,
            PositionCode: "",
            ClassType: 0,
            DelFlag: 0,
            SortOrder: 0
        },
        tablename: "BaseSymptomClass",         //表名
        key: "ClassId",                  //主键字段
        val: "0",                   //主键值
        objval: {},                 //实体值
        where: "",                  //查询条件
        pageinfo: {},               //分页对象 
    }

    getDeptList() {
        var deptName = TSLobsterAPI.Program.RequestQuery("deptName", "");
        var delFlag = TSLobsterAPI.Program.RequestQuery("delFlag", -1);

        var pageInfo = {
            key: "DeptId",
            page: TSLobsterAPI.Program.RequestQuery('page', 0),
            perPage: TSLobsterAPI.Program.RequestQuery('limit', 10),
            totalRecord: 0,
            totalPage: 0
        }

        var dbparams = {
            workid: TSLobsterAPI.Program.GetWorkId(),
            deptName: deptName,
            delFlag: delFlag
        }
        var strsql = "select a.*, \
        isnull((select WorkDeptId from  HG_DeptToWork where DeptCode=a.Code and WorkId=@workid),'') as WorkDeptId\
        ,isnull((select WorkDeptName from  HG_DeptToWork where DeptCode=a.Code and WorkId=@workid),'') as WorkDeptName\
         from basedept a \
        where 1=1";

        if (deptName != "") {
            strsql += " and a.Name like @deptName ";
        }
        if (parseInt(delFlag) > -1) {
            strsql += "  and a.DelFlag=@delFlag ";
        }
        return { list: TSLobsterAPI.DbHelper.DbGetJsonData(this.data.dbConn, strsql, dbparams, pageInfo), count: pageInfo.totalRecord };
    }

    updateDeptState() {
        var deptId = TSLobsterAPI.Program.RequestQuery("deptId", 0);
        var status = TSLobsterAPI.Program.RequestQuery("status", 0);
        var strsql = "update BaseDept set DelFlag=@status where deptId=@deptId"
        var dbparams = {
            deptId: deptId,
            status: status
        }

        TSLobsterAPI.DbHelper.DbExecuteUpdate(this.data.dbConn, strsql, dbparams);
        return { result: true };
    }

    saveDept() {
        var dept = JSON.parse(TSLobsterAPI.Program.RequestBody());
        var strsql = "select count(1) val from BaseDept where Name=@Name and DeptId<>@DeptId";
        var dbparams = {
            Name: dept.Name,
            DeptId: dept.DeptId
        }

        var res = TSLobsterAPI.DbHelper.DbGetJsonObject(this.data.dbConn, strsql, dbparams);
        if (parseInt(res.val) > 0) {
            throw "已经存在" + dept.Name + "科室";
        }


        this.DeptModel.objval = dept;
        TSLobsterAPI.DbHelper.SaveModel(this.data.dbConn, this.DeptModel);

        return { result: true };
    }


    getPositionList() {
        var strsql = "select * from BasePosition ";
        var data = TSLobsterAPI.DbHelper.DbGetJsonData(this.data.dbConn, strsql);
        return { positionList: data };
    }

    savePostion() {
        var position = JSON.parse(TSLobsterAPI.Program.RequestBody());
        var strsql = "select count(1) val from BasePosition where (PositionCode=@PositionCode or PositionName=@PositionName) and ID<>@ID";
        var dbparams = {
            PositionCode: position.PositionCode,
            PositionName: position.PositionName,
            ID: position.ID
        }

        var ret = TSLobsterAPI.DbHelper.DbGetJsonObject(this.data.dbConn, strsql, dbparams);
        if (ret.val > 0) {
            throw "已经存在该【" + position.PositionCode + ":" + position.PositionName + "】部位";
        }

        this.PositionModel.objval = position;

        TSLobsterAPI.DbHelper.SaveModel(this.data.dbConn, this.PositionModel);
    }

    getSymptomList() {
        var classId = TSLobsterAPI.Program.RequestQuery("classId", "");
        var pid = TSLobsterAPI.Program.RequestQuery("pid", "");

        var pageInfo = {
            key: "SymId",
            page: TSLobsterAPI.Program.RequestQuery('page', 0),
            perPage: TSLobsterAPI.Program.RequestQuery('limit', 10),
            totalRecord: 0,
            totalPage: 0
        }

        var dbparams = {
            pid: pid,
            classId: classId
        }
        var strsql = "select a.*, b.ClassName,c.PositionName,c.ID as PosID,c.PositionCode,d.Name as DeptName from [BaseSymptom] a left join [BaseSymptomClass] b on a.ClassId=b.ClassId\
        left join [BasePosition] c on b.PositionCode=c.PositionCode\
        left join BaseDept d on a.DeptCode=d.Code where 1=1";
        if (parseInt(pid) > 0) {
            strsql += " and (c.ID=@pid or c.ParentId=@pid) ";
        }
        if (parseInt(classId) > 0) {
            strsql += "  and a.ClassId=@classId ";
        }
        return { list: TSLobsterAPI.DbHelper.DbGetJsonData(this.data.dbConn, strsql, dbparams, pageInfo), count: pageInfo.totalRecord };
    }

    getSymptomClassList() {

        var strsql = "select * from BaseSymptomClass where 1=1";
        var strsql2 = "select * from BaseDept where 1=1";

        var symptomClassList = TSLobsterAPI.DbHelper.DbGetJsonData(this.data.dbConn, strsql);
        var deptList = TSLobsterAPI.DbHelper.DbGetJsonData(this.data.dbConn, strsql2);
        return { symptomClassList: symptomClassList, deptList: deptList };
    }

    saveSymptom() {
        var symptom = JSON.parse(TSLobsterAPI.Program.RequestBody());

        this.SymptomModel.objval = symptom;
        TSLobsterAPI.DbHelper.SaveModel(this.data.dbConn, this.SymptomModel);
    }

    updateSymptomState() {
        var id = TSLobsterAPI.Program.RequestQuery("id", 0);
        var status = TSLobsterAPI.Program.RequestQuery("status", 0);

        var strsql = "update BaseSymptom set DelFlag=@status where SymId=@id"
        var dbparams = {
            id: id,
            status: status
        }
        TSLobsterAPI.DbHelper.DbExecuteUpdate(this.data.dbConn, strsql, dbparams);
    }

    saveSymptomClass() {
        var symptomClass = JSON.parse(TSLobsterAPI.Program.RequestBody());

        this.SymptomClassModel.objval = symptomClass;
        TSLobsterAPI.DbHelper.SaveModel(this.data.dbConn, this.SymptomClassModel);
    }

    updateSymptomClassState() {
        var ClassId = TSLobsterAPI.Program.RequestQuery("ClassId", 0);
        var status = TSLobsterAPI.Program.RequestQuery("status", 0);
        var strsql = "update BaseSymptomClass set DelFlag=@status where ClassId=@ClassId"
        var dbparams = {
            ClassId: ClassId,
            status: status
        }
        TSLobsterAPI.DbHelper.DbExecuteUpdate(this.data.dbConn, strsql, dbparams);
        return { result: true };
    }
}